package com.heaven.validation;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class EnumValueValidator implements ConstraintValidator<EnumValue, Object> {
    private final Set<Object> enumValues = new HashSet<>();
    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        return enumValues.contains(value);
    }

    @Override
    public void initialize(EnumValue constraintAnnotation) {
        int[] intValues = constraintAnnotation.intValues();
        String[] strValues = constraintAnnotation.strValues();
        for (int intValue : intValues) {
            enumValues.add(intValue);
        }
        enumValues.addAll(Arrays.asList(strValues));
    }
}
